// pages/discussion/discussion.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pictures: [],
    colorTab:0,
    focus:false,
    cursor:0,
    inputHidden:true,
    Value:'',
    neirong:'',
    cid:0,
    ctype: 0
  },
  button: function (e) {
    console.log(this.data.neirong)
  },
  //失去焦点
  bindReplaceInput:function (e) {
    var that = this;
    that.setData({
      inputHidden: true,
      focus: false,
      Value:''
    })
  },
  //回复
  pinglun:function (e) {
    var that = this;
    var cid = e.currentTarget.dataset.cid;
    that.setData({
      inputHidden: false,
      focus:true,
      Value:'',
      cursor:0,
      cid:cid,
      
    })
  },
  //输入
  bindKeyInput:function (e) {
    var that = this;
    // console.log(e.detail.value)
    that.setData({
      cursor: e.detail.cursor,
      neirong: e.detail.value
    })
  },
  //tab
  fabuTab:function (e)  {
    var that = this;
    that.setData({
      colorTab: e.target.id
    })
  },
  //预览
  previewImage: function (e) {
    var that = this,
      //获取当前图片的下表
      index = e.currentTarget.dataset.index,
      //数据源
      pictures = this.data.pictures;
    wx.previewImage({
      //当前显示下表
      current: pictures[index],
      //数据源
      urls: pictures
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ctype = options.ctype;
    var that = this;
    that.setData({
      ctype:ctype
    });
    
  },

  send:function (e) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Community/send',
      method: "post",
      data: {
        cid: that.data.cid,
        uid: app.d.userId,
        content:that.data.neirong
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        
        if (res.data.status == 1) {
          that.setData({
            list: res.data.list
          });
          setTimeout(function () {
            that.refresh();
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var ctype = that.data.ctype;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Community/index',
      method: "post",
      data: {
        ctype: ctype

      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          that.setData({
            list: res.data.list,
          });
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },
  refresh:function (){
    var that = this;
    console.log(that.data.ctype)
    wx.request({
      url: app.d.ceshiUrl + '/Api/Community/index',
      method: "post",
      data: {
        ctype: that.data.ctype

      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          that.setData({
            list: res.data.list
          });
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },

  search:function (e){
    var that = this;
    var ctype = that.data.ctype;
    var type2 = e.currentTarget.dataset.ctype;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Community/search',
      method: "post",
      data: {
        ctype: ctype,
        type2:type2,
        uid:app.d.userId
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          that.setData({
            list: res.data.list,
          });
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})